package com.example.myfirst.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.myfirst.dao.GoodsDao;
import com.example.myfirst.entity.Goods;
import com.example.myfirst.service.GoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsDao goodsDao;

    @ApiModelProperty("获取所有商品信息")
    @Override
    public IPage<Goods> queryAllGoods(String goodsName, Integer CategoryId, Integer page, Integer size) {
        // 定义分页规则
        IPage<Goods> iPage = new Page<>(page, size);
        // 2.分页查询
        // 封装条件对象
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq(CategoryId != null && CategoryId != 0, "category_id", CategoryId);
        qw.eq(goodsName != null && !goodsName.equals(""), "goods_name", goodsName);
        return goodsDao.selectPage(iPage, qw);
    }

    @ApiModelProperty("根据id获取商品信息")
    @Override
    public Goods queryGoodsById(Integer goodsId) {
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("goods_id", goodsId);
        return goodsDao.selectOne(qw);
    }

    @Override
    public List<Goods> queryGoodsByGoodsName(String goodsName) {
        QueryWrapper qw = new QueryWrapper<>();
        qw.like("goods_name", goodsName);
        return goodsDao.selectList(qw);
    }

    @ApiModelProperty("添加商品")
    @Override
    public Boolean addGoods(Goods goods) {
        int i = goodsDao.insert(goods);
        return i > 0;
    }

    @ApiModelProperty("根据id删除商品")
    @Override
    public Boolean delGoods(Integer id) {
        int i = goodsDao.deleteById(id);
        return i > 0;
    }

    @ApiModelProperty("根据id修改商品")
    @Override
    public Boolean updateGoodsById(Goods goods, Integer id) {
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("id", id);
        int i = goodsDao.update(goods, qw);
        return i > 0;
    }

    @ApiModelProperty("根据系列id获取所有的商品")
    @Override
    public List<Goods> queryGoodsByCategoryId(Integer category_id) {
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("category_id", category_id);
        return goodsDao.selectList(qw);
    }
}
